<template>
    <div  style="width:700px;margin-top: 20px;">
        <div class="roleAddClass">
            <el-input placeholder="请输入角色英文名" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input  placeholder="请输入角色中文名" v-model="role.nameZh" @keydown.enter.native="doAddRole">
            </el-input>
            <el-button type="primary"  icon="el-icon-plus" @click="doAddRole">添加角色</el-button>
        </div>
        <el-table
                stripe
                :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
                style="width: 100%">
            <el-table-column v-if="idHidden"
                    label="ID"
                    prop="id">
            </el-table-column>
            <el-table-column
                    label="身份"
                    prop="nameZh">
            </el-table-column>
            <el-table-column
                    label="Name"
                    prop="name">
            </el-table-column>
            <el-table-column
                    align="right">
                <template slot="header" slot-scope="scope">
                    <el-input
                            v-model="search"
                            size="mini"
                            placeholder="输入关键字搜索"/>
                </template>
                <template slot-scope="scope">
                    <!--
                    <el-popover
                            placement="right"
                            width="800"
                            v-model="dialogPopoverVisible"
                            trigger="manual">
                        <div class="roleAddClass">
                            <span style="margin-top: 5px;margin-right: 3px;">角色英文名: </span><el-input v-model="changeRole.name"></el-input>
                            <span style="margin-top: 5px;margin-right: 3px;">角色中文名:</span><el-input   v-model="changeRole.nameZh"></el-input>
                        </div>
                        <div style="width: 750px;height: 300px;">

                            <div >
                                <el-tree
                                        show-checkbox
                                        default-expand-all
                                        node-key="id"
                                        :ref="myTree"
                                        :default-checked-keys="selectedMenus"
                                        :data="allMenus" :props="defaultProps"
                                        @check-change="handleCheckChange"></el-tree>
                            </div>
                            <div style="display: flex;justify-content: center;">
                                <div>
                                    <el-button type="primary" @click="doUpdate(changeRole.id)">保存</el-button>
                                    <el-button type="primary" @click="dialogPopoverVisible = false">取消</el-button>
                                </div>
                            </div>
                        </div>
                    </el-popover>
                    -->
                    <el-dialog title="修改权限" :visible.sync="dialogPopoverVisible" style="text-align: left">
                        <div class="roleAddClass">
                            <span style="margin-top: 5px;margin-right: 3px;">角色英文名: </span><el-input :disabled = "true" v-model="changeRole.name"></el-input>
                            <span style="margin-top: 5px;margin-right: 3px;">角色中文名:</span><el-input  :disabled = "true" v-model="changeRole.nameZh"></el-input>
                        </div>
                        <div style="width: 750px;">

                            <div >
                                <el-tree
                                        v-loading="loading"
                                        show-checkbox
                                        default-expand-all
                                        node-key="id"
                                        :ref="myTree"
                                        :default-checked-keys="selectedMenus"
                                        :data="allMenus" :props="defaultProps"
                                        @check-change="handleCheckChange"></el-tree>
                            </div>
                            <div style="display: flex;justify-content: center;">
                                <div>
                                    <el-button type="primary" @click="doUpdate(changeRole.id)">保存</el-button>
                                    <el-button type="primary" @click="dialogPopoverVisible = false">取消</el-button>
                                </div>
                            </div>
                        </div>
                    </el-dialog>
                    <el-button slot="reference" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
    export default {
        name: "SysSecu",

        data() {
            return {
                role:{
                    name:'',
                    nameZh:''
                },
                dialogPopoverVisible:false,
                loading:true,
                idHidden: true,
                roles:[],
                myTree:'',
                changeRole:{
                    id:1,
                    name:'',
                    nameZh:''
                },
                tableData: [],
                search: '',
                allMenus:[],
                selectedMenus:[],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        mounted() {
            this.initRoles();
        },
        methods: {
            doUpdate(id,index){
                let tr = this.myTree;
                let tree = this.$refs[`${tr}`];
                let selectedKeys = tree.getCheckedKeys(true);
                let url='/system/role/updateRole/?rid='+id;
                selectedKeys.forEach(key=>{
                    url += '&mids='+key;
                })
                this.putRequest(url).then(resp=>{
                    if(resp){
                        this.dialogPopoverVisible = false;
                        //this.initRoles();
                        //this.activeName = -1;
                    }
                })
            },
            handleCheckChange(data, checked, indeterminate){

            },
            doAddRole(){
                if(this.role.name && this.role.nameZh){
                    this.postRequest("/system/role/addRoles",this.role).then(resp=>{
                        if(resp){
                            this.role.name='';
                            this.role.nameZh='';
                            this.initRoles();
                        }

                    })
                }else{
                    this.$message.error("数据不能为空！！");
                }
            },
            handleEdit(index, row) {
                console.log(row);
                this.changeRole.id = row.id;
                this.changeRole.name = row.name;
                this.changeRole.nameZh = row.nameZh;
                this.myTree = "tree"+row.id+Date.now();
                this.roles = row;
                this.dialogPopoverVisible = true;
                this.loading = true;
                this.initAllMenus();
                this.initSelectedMenus(row.id);
            },
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除['+row.nameZh+']角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    this.deleteRequest("/system/role/deleteRole/"+row.id).then(resp=>{
                        if(resp){
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            initSelectedMenus(rid){
                this.getRequest("/system/menu/menuRid/"+rid).then(resp=>{
                    if(resp){
                        this.selectedMenus = resp;
                        this.loading = false;
                    }

                });
            },
            initAllMenus(){
                this.getRequest("/system/menu/allMenus").then(resp=>{
                    if(resp){
                        this.allMenus = resp;

                    }
                })
            },
            initRoles(){
                this.getRequest("/system/role/initRoles").then(resp=>{
                    if(resp){
                        this.tableData = resp.data;

                    }
                })
            }
        }
    }
</script>

<style scoped>
    .roleAddClass{
        display: flex;
        justify-content: flex-start;
        margin-bottom: 20px;
    }
    .roleAddClass .el-input{
        width: 280px;
        margin-right: 6px;
    }
</style>